package com.buaa.daas.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.buaa.daas.entity.StuInfo;
import com.buaa.daas.service.StuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangKangMing
 * @since 2021-06-04
 */
@Controller
public class StuInfoController {

    @Autowired
    StuInfoService stuInfoService;

    @GetMapping("/info")
    String infoForm(HttpServletRequest request, Model model){
        StuInfo stuInfo = stuInfoService.getInfoByRequest(request);
        if(stuInfo!=null){
            String stuNum = stuInfo.getStuNum();//学号
            String stuName = stuInfo.getStuName();//姓名
            String stuEmail = stuInfo.getStuEmail();//邮箱
            String stuEaisess = stuInfo.getStuEaisess();//eaisess值

            HashMap<String, String> map = new HashMap<>();
            map.put("stuNum",stuNum!=null?stuNum:"");
            map.put("stuName",stuName!=null?stuName:"");
            map.put("stuEmail",stuEmail!=null?stuEmail:"");
            map.put("stuEaisess",stuEaisess!=null?stuEaisess:"");

            model.addAllAttributes(map);
        }

        return "stuinfo";
    }

    @PostMapping("/info")
    String postInfoForm(HttpServletRequest request, @RequestParam Map<String,Object> params, Model model){
        StuInfo stuInfo = stuInfoService.getInfoByRequest(request);
        String stuEmail = (String) params.get("stuEmail");
        String stuEaisess = (String) params.get("stuEaisess");

        stuInfo.setStuEmail(stuEmail);
        stuInfo.setStuEaisess(stuEaisess);
        stuInfoService.updateById(stuInfo);

        if(stuInfo!=null){
            String stuNum = stuInfo.getStuNum();//学号
            String stuName = stuInfo.getStuName();//姓名

            HashMap<String, String> map = new HashMap<>();
            map.put("stuNum",stuNum!=null?stuNum:"");
            map.put("stuName",stuName!=null?stuName:"");
            map.put("stuEmail",stuEmail!=null?stuEmail:"");
            map.put("stuEaisess",stuEaisess!=null?stuEaisess:"");

            model.addAllAttributes(map);
        }

        return "stuinfo";
    }

}

